package thread;

//yield 让位，屈服

public class Test12_yield {
    public static void main(String[] args) {
        YieldOne y = new YieldOne();
        YieldOne y1 = new YieldOne();

        Thread t1 = new Thread(y,"a");
        Thread t2 = new Thread(y1,"b");

        t1.setPriority(1);
        t1.start();

        t2.setPriority(10);
        t2.start();
    }
}

class YieldOne implements Runnable{
    @Override
    public void run() {
        if ("a".equals(Thread.currentThread().getName())){
            Thread.yield();//yield指挥将执行权放给优先级高的线程
//            try {
//                Thread.sleep(100);//sleep不管优先级，只要调用则当前线程睡眠，其他线程接过执行权
//            }catch (InterruptedException e){
//                e.printStackTrace();
//            }
        }
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}